//定义小仓库
import { defineStore } from 'pinia';

//引入数据泛型
import type { categoryStoreStateType } from './interface/category';

//引入请求函数
import { reqC1List, reqC2List, reqC3List, } from '@/api/product/attr/attr';

let useCategoryStore = defineStore('category', {
    //储存数据
    state: (): categoryStoreStateType => ({
        //存储一级分类的数据
        c1List: [],
        //存储二级分类的数据
        c2List: [],
        //存储三级分类的数据
        c3List: [],
        //收集一级分类的ID
        c1Id: '',
        //收集二级分类的ID
        c2Id: '',
        //收集三级分类的ID
        c3Id: '',
    }),
    //处理业务的地方
    actions: {
        async getC1List() {
            let result = await reqC1List();
            this.c1List = result;
        },
        //一级分类下拉菜单触发change事件，获取二级分类数据
        async getC2List() {
            //清空二级三级内容
            this.c2Id = '';
            this.c3Id = '';

            this.c2List = [];
            this.c3List = [];
            let result = await reqC2List(this.c1Id);
            this.c2List = result;
        },
        //二级分类下拉菜单change事件，获取三级分类数据
        async getC3List(){
            //清空三级分类
            this.c3Id = '';
            
            this.c3List = [];
            let result = await reqC3List(this.c2Id);
            this.c3List = result;
        }
    }
});
//对外暴露
export default useCategoryStore;